package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.impl.CourseServiceimpl;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/course")
public class Couse extends BaseServlet {
    //写方法名
    CourseService courseService = new CourseServiceimpl();

    public void findCourseList(HttpServletRequest req, HttpServletResponse resp) {
        try {

            resp.getWriter().println("hello");
        } catch (IOException e) {
            e.printStackTrace();
        }
//        //获取课程列表数据
//        List<Couse> course = courseService.findCourse();
//        //对象转换为JSON 返还给浏览器
//        String s = JSON.toJSONString(course);
//        //返还给浏览器
//        try {
//            resp.getWriter().println(s);
//        } catch (IOException e) {
//
//        }

    }
    //条件查询结果
    public void findCourseNameAndStatus(HttpServletRequest req, HttpServletResponse resp){

        try {
            //获取参数
            String course_name = req.getParameter("course_name");
            String status = req.getParameter("status");
            //调用service 层方法
            CourseServiceimpl courseServiceimpl = new CourseServiceimpl();
            List<Course> cs = courseService.findCourseNameAndStatus(course_name, status);

            //转json
            String s =JSON.toJSONString(cs);
            resp.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
